package com.yyq.adms.sysmanage.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.sysmanage.bpo.DataDictionaryBpo;
import com.yyq.adms.sysmanage.pojo.DataDictionary;
import com.yyq.adms.yyb.advance.pojo.STApply;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.page.PageFormData;


@Controller("dataDictionaryAction")
@Scope("prototype")
public class DataDictionaryAction extends BaseAction {

	DataDictionary po = new DataDictionary();
	private DataDictionaryBpo bpo ;
	
	@Resource(name="dataDictionaryBpo")
	public void setBpo(DataDictionaryBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	public String execute()throws Exception
	{
		super.setBpo(bpo);
		super.setObj(po);
		String s = super.execute();
		if(super.getObj()  instanceof DataDictionary) {
			this.setPo((DataDictionary)super.getObj());
		}
		return s;
	}

	public DataDictionary getPo() {
		return po;
	}

	public void setPo(DataDictionary po) {
		this.po = po;
	}

	@Override
	public String data() {
		String sql = "SELECT a.id,a.yyq_key,a.yyq_TEXT,a.yyq_VALUE,a.yyq_DESCRIPTION,a.yyq_PARENT_ID,b.yyq_key as parentKey " +
				" from yyq_sys_datadictionary a LEFT JOIN yyq_sys_datadictionary b "+
				"on a.yyq_PARENT_ID=b.id where a.isEnabled=1";
		PageFormData pfd = bpo.searchSqlPage(sql,this.getPageForm().getPageNo(), this.getPageForm().getPageSize());
		String[] metas = new String[]{"Id","Key","Text","Value","Description","ParentId","ParentKey" };
		String[] ts = new String[]{"LONG","STR","STR","STR","STR","LONG","STR"};
		pfd.setData(this.pressData(pfd.getData(), ts, metas, DataDictionary.class));
		this.setFormData(pfd);
		return ActionType.ACTION_DATA_LIST;
	}

	/*@Override
	public String goAdd() {
		String hql = "select DISTINCT r from DataDictionary where isEnabled=1" +
				"";
		return super.goAdd();
	}

	@Override
	public String goUpdate() {
		// TODO Auto-generated method stub
		return super.goUpdate();
	}*/
	
	
	@Override
	public String view() {
		String[] flags = (String[])this.getFormValue("flag");
		if(flags != null){
			String flag = flags[0];
			LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
			if("department".equals(flag)){
				if(user != null){
					this.setData(bpo.getDepts(user));
				}
			}else if("reviewPeriod".equals(flag))
			{
				String[] ids = (String[])this.getFormValue("sourceId");
System.out.println(ids);
				if(ids != null && ids.length>0){
System.out.println(ids[0]);
					bpo.setCls(STApply.class);
					this.setObj(bpo.findByPk(Long.parseLong(ids[0])));
				}
				System.out.println(this.getObj());
			}else if("company".equals(flag)){
				this.setData(bpo.getCompany());
			}
			else{
				this.setData(bpo.search("from "+po.getClass().getName()+" where key='"+flag+"'"));
			}
		}
		return ActionType.ACTION_VIEW;
	}
	
}
